package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.wangjun.app.easypermissionslibrary.permission.AskagainCallback;
import com.wangjun.app.easypermissionslibrary.permission.FullCallback;
import com.wangjun.app.easypermissionslibrary.permission.PermissionEnum;
import com.wangjun.app.easypermissionslibrary.permission.PermissionHelper;
import com.wangjun.app.easypermissionslibrary.permission.PermissionManager;
import com.ydkj.a37e_mall.Classes.Fragment.FindShopFg;
import com.ydkj.a37e_mall.Classes.Fragment.HomePageFg;
import com.ydkj.a37e_mall.Classes.Fragment.MyFg;
import com.ydkj.a37e_mall.Classes.Fragment.ShoppingCarFg;
import com.ydkj.a37e_mall.Classes.Fragment.SlidingMenu_fg;
import com.ydkj.a37e_mall.Classes.Fragment.SortFg;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import java.util.ArrayList;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;

    private LinearLayout lly_homepage, lly_sort, lly_findshop, lly_shoppingcar, lly_my;
    private ImageView img_homepage, img_sort, img_findshop, img_shoppingcar, img_my;
    private TextView tv_homepage, tv_sort, tv_findshop, tv_shoppingcar, tv_my;
    private HomePageFg homePageFg;
    private SortFg sortFg;
    private FindShopFg findShopFg;
    private ShoppingCarFg shoppingCarFg;
    private MyFg myFg;
    private FragmentManager fragmentManager;
    private SlidingMenu_fg slidingMenufg;

    private MyLocationConfiguration.LocationMode mCurrentMode;
    private LocationClient mLocClient; // 定位相关
    public MyLocationListenner myListener = new MyLocationListenner();
    boolean isFirstLoc = true;// 是否首次定位


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SDKInitializer.initialize(getApplicationContext());

        setContentView(R.layout.activity_main);

        mActivity = this;
//        initWindow();//沉浸式状态栏
        init();
        initMenu();//初始化侧滑菜单Fg页面
        askMultiPermission();
    }

    private void init() {
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);


        initmapview();

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        tv_homepage = (TextView) mActivity.findViewById(R.id.tv_homepage);
        tv_sort = (TextView) mActivity.findViewById(R.id.tv_sort);
        tv_findshop = (TextView) mActivity.findViewById(R.id.tv_findshop);
        tv_shoppingcar = (TextView) mActivity.findViewById(R.id.tv_shoppingcar);
        tv_my = (TextView) mActivity.findViewById(R.id.tv_my);

        img_homepage = (ImageView) mActivity.findViewById(R.id.img_homepage);
        img_sort = (ImageView) mActivity.findViewById(R.id.img_sort);
        img_findshop = (ImageView) mActivity.findViewById(R.id.img_findshop);
        img_shoppingcar = (ImageView) mActivity.findViewById(R.id.img_shoppingcar);
        img_my = (ImageView) mActivity.findViewById(R.id.img_my);

        lly_homepage = (LinearLayout) mActivity.findViewById(R.id.lly_homepage);
        lly_sort = (LinearLayout) findViewById(R.id.lly_sort);
        lly_findshop = (LinearLayout) mActivity.findViewById(R.id.lly_findshop);
        lly_shoppingcar = (LinearLayout) mActivity.findViewById(R.id.lly_shoppingcar);
        lly_my = (LinearLayout) mActivity.findViewById(R.id.lly_my);


        homePageFg = new HomePageFg();
        myFg = new MyFg();
        sortFg = new SortFg();
        findShopFg = new FindShopFg();
        shoppingCarFg = new ShoppingCarFg();

        lly_homepage.setSelected(true);
        tv_homepage.setSelected(true);

        ft.add(R.id.fly_main, homePageFg, "one");
        ft.commitAllowingStateLoss();

        lly_homepage.setOnClickListener(this);
//        lly_sort.setOnClickListener(this);
        lly_findshop.setOnClickListener(this);
        lly_shoppingcar.setOnClickListener(this);
        lly_my.setOnClickListener(this);
    }


    private void initmapview() {

        //定位相关
        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;

        // 定位初始化
        mLocClient = new LocationClient(mActivity);

        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(30000);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    /**
     * 初始化侧滑菜单Fg页面
     */
    private void initMenu() {
        slidingMenufg = new SlidingMenu_fg();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(slidingMenufg, "fifth");
        ft.commit();
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.A000000), 0);

    }

    @Override
    public void onClick(View view) {
        FragmentTransaction ft;
        switch (view.getId()) {
            case R.id.lly_homepage://首页

                ft = fragmentManager.beginTransaction();
                if (lly_homepage.isSelected()) {
                    return;
                }
                if (sortFg.isAdded()) {
                    ft.hide(sortFg);
                    sortFg.onPause();
                }
                if (findShopFg.isAdded()) {
                    ft.hide(findShopFg);
                    findShopFg.onPause();
                }
                if (shoppingCarFg.isAdded()) {
                    ft.hide(shoppingCarFg);
                    shoppingCarFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.hide(myFg);
                    myFg.onPause();
                }
                ft.show(homePageFg);
                ft.commit();

                lly_homepage.setSelected(true);
                lly_sort.setSelected(false);
                lly_findshop.setSelected(false);
                lly_shoppingcar.setSelected(false);
                lly_my.setSelected(false);

                img_homepage.setImageResource(R.mipmap.homepage);
                img_sort.setImageResource(R.mipmap.sort_default);
                img_findshop.setImageResource(R.mipmap.find_deafult);
                img_shoppingcar.setImageResource(R.mipmap.shoppingcar_deafult);
                img_my.setImageResource(R.mipmap.my_deafult);

                tv_homepage.setSelected(true);
                tv_sort.setSelected(false);
                tv_findshop.setSelected(false);
                tv_shoppingcar.setSelected(false);
                tv_my.setSelected(false);
                break;
            case R.id.lly_sort://分类

                if (lly_sort.isSelected()) {
                    return;
                }
                ft = fragmentManager.beginTransaction();

                if (homePageFg.isAdded()) {
                    ft.hide(homePageFg);
                    homePageFg.onPause();
                }
                if (findShopFg.isAdded()) {
                    ft.hide(findShopFg);
                    findShopFg.onPause();
                }
                if (shoppingCarFg.isAdded()) {
                    ft.hide(shoppingCarFg);
                    shoppingCarFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.hide(myFg);
                    myFg.onPause();
                }
                if (sortFg.isAdded()) {
                    ft.show(sortFg);
                } else {
                    ft.add(R.id.fly_main, sortFg, "two");
                    ft.show(sortFg);
                }
                ft.commit();

                lly_homepage.setSelected(false);
                lly_sort.setSelected(true);
                lly_findshop.setSelected(false);
                lly_shoppingcar.setSelected(false);
                lly_my.setSelected(false);

                img_homepage.setImageResource(R.mipmap.homepage_default);
                img_sort.setImageResource(R.mipmap.sort);
                img_findshop.setImageResource(R.mipmap.find_deafult);
                img_shoppingcar.setImageResource(R.mipmap.shoppingcar_deafult);
                img_my.setImageResource(R.mipmap.my_deafult);

                tv_homepage.setSelected(false);
                tv_sort.setSelected(true);
                tv_findshop.setSelected(false);
                tv_shoppingcar.setSelected(false);
                tv_my.setSelected(false);
                break;
            case R.id.lly_findshop://找店

                if (lly_findshop.isSelected()) {
                    return;
                }
                ft = fragmentManager.beginTransaction();
                if (homePageFg.isAdded()) {
                    ft.hide(homePageFg);
                    homePageFg.onPause();
                }
                if (sortFg.isAdded()) {
                    ft.hide(sortFg);
                    sortFg.onPause();
                }
                if (shoppingCarFg.isAdded()) {
                    ft.hide(shoppingCarFg);
                    shoppingCarFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.hide(myFg);
                    myFg.onPause();
                }
                if (findShopFg.isAdded()) {
                    ft.show(findShopFg);
                } else {
                    ft.add(R.id.fly_main, findShopFg, "three");
                    ft.show(findShopFg);
                }
                ft.commit();

                lly_homepage.setSelected(false);
                lly_sort.setSelected(false);
                lly_findshop.setSelected(true);
                lly_shoppingcar.setSelected(false);
                lly_my.setSelected(false);

                img_homepage.setImageResource(R.mipmap.homepage_default);
                img_sort.setImageResource(R.mipmap.sort_default);
                img_findshop.setImageResource(R.mipmap.find);
                img_shoppingcar.setImageResource(R.mipmap.shoppingcar_deafult);
                img_my.setImageResource(R.mipmap.my_deafult);

                tv_homepage.setSelected(false);
                tv_sort.setSelected(false);
                tv_findshop.setSelected(true);
                tv_shoppingcar.setSelected(false);
                tv_my.setSelected(false);
                break;
            case R.id.lly_shoppingcar://购物车

                if (lly_shoppingcar.isSelected()) {
                    return;
                }
                ft = fragmentManager.beginTransaction();
                if (homePageFg.isAdded()) {
                    ft.hide(homePageFg);
                    homePageFg.onPause();
                }
                if (sortFg.isAdded()) {
                    ft.hide(sortFg);
                    sortFg.onPause();
                }
                if (findShopFg.isAdded()) {
                    ft.hide(findShopFg);
                    findShopFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.hide(myFg);
                    myFg.onPause();
                }
                if (shoppingCarFg.isAdded()) {
                    ft.show(shoppingCarFg);
                } else {
                    ft.add(R.id.fly_main, shoppingCarFg, "four");
                    ft.show(shoppingCarFg);
                }
                ft.commit();

                lly_homepage.setSelected(false);
                lly_sort.setSelected(false);
                lly_findshop.setSelected(false);
                lly_shoppingcar.setSelected(true);
                lly_my.setSelected(false);

                img_homepage.setImageResource(R.mipmap.homepage_default);
                img_sort.setImageResource(R.mipmap.sort_default);
                img_findshop.setImageResource(R.mipmap.find_deafult);
                img_shoppingcar.setImageResource(R.mipmap.shopping_car_red);
                img_my.setImageResource(R.mipmap.my_deafult);

                tv_homepage.setSelected(false);
                tv_sort.setSelected(false);
                tv_findshop.setSelected(false);
                tv_shoppingcar.setSelected(true);
                tv_my.setSelected(false);
                break;
            case R.id.lly_my://我的

                if (lly_my.isSelected()) {
                    return;
                }
                ft = fragmentManager.beginTransaction();
                if (homePageFg.isAdded()) {
                    ft.hide(homePageFg);
                    homePageFg.onPause();
                }
                if (sortFg.isAdded()) {
                    ft.hide(sortFg);
                    sortFg.onPause();
                }
                if (findShopFg.isAdded()) {
                    ft.hide(findShopFg);
                    findShopFg.onPause();
                }
                if (shoppingCarFg.isAdded()) {
                    ft.hide(shoppingCarFg);
                    shoppingCarFg.onPause();
                }
                if (myFg.isAdded()) {
                    ft.show(myFg);
                } else {
                    ft.add(R.id.fly_main, myFg, "four");
                    ft.show(myFg);
                }
                ft.commit();

//                myFg.getEditText(new MyFg.CallBack() {
//                    @Override
//                    public void getResult(String result) {
//                        Log.e("ssss", "测试===》" + result);
//                    }
//                });

                lly_homepage.setSelected(false);
                lly_sort.setSelected(false);
                lly_findshop.setSelected(false);
                lly_shoppingcar.setSelected(false);
                lly_my.setSelected(true);

                img_homepage.setImageResource(R.mipmap.homepage_default);
                img_sort.setImageResource(R.mipmap.sort_default);
                img_findshop.setImageResource(R.mipmap.find_deafult);
                img_shoppingcar.setImageResource(R.mipmap.shoppingcar_deafult);
                img_my.setImageResource(R.mipmap.my);

                tv_homepage.setSelected(false);
                tv_sort.setSelected(false);
                tv_findshop.setSelected(false);
                tv_shoppingcar.setSelected(false);
                tv_my.setSelected(true);
                break;
        }
    }


    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null)
                return;
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
//            Log.e("ssss", "经度 mian》" + location.getLongitude());
//            Log.e("ssss", "纬度 mian》" + location.getLatitude());

            sharedPreferencesUtil.setJingdu(location.getLongitude() + "");
            sharedPreferencesUtil.setWeidu(location.getLatitude() + "");
//            //定位成功后宝保存，并关闭定位
//            mLocClient.stop();

        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    }

    /**
     * 多个权限的检查
     */
    private void askMultiPermission() {


        PermissionManager.with(MainActivity.this)
                .key(800)
                .permission(PermissionEnum.RECORD_AUDIO, PermissionEnum.ACCESS_COARSE_LOCATION, PermissionEnum.ACCESS_FINE_LOCATION,
                        PermissionEnum.READ_PHONE_STATE, PermissionEnum.WRITE_EXTERNAL_STORAGE, PermissionEnum.CAMERA,PermissionEnum.READ_EXTERNAL_STORAGE)
                .askagain(true)
                .askagainCallback(new AskagainCallback() {
                    @Override
                    public void showRequestPermission(UserResponse response) {
                        showDialog(response);
                    }
                })
                .callback(new FullCallback() {
                    @Override
                    public void grated(ArrayList<PermissionEnum> permissionsGranted) {
//                        Toast.makeText(mActivity, "权限被允许了", Toast.LENGTH_SHORT).show();
                        Log.e("url","权限被允许了");
                    }

                    @Override
                    public void deniedSetting(ArrayList<PermissionEnum> permissionsDenied) {
                        PermissionHelper.openApplicationSettings(MainActivity.this, R.class.getPackage().getName());
                    }

                    @Override
                    public void deniedCancle(ArrayList<PermissionEnum> permissionsDenied) {
                        finish();
                    }
                })
                .ask();
    }

    private void showDialog(final AskagainCallback.UserResponse response) {
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("请求权限")
                .setMessage("需要调用系统权限，否则APP部分功能无法使用")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        response.result(true);
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        response.result(false);
                    }
                })
                .show();
    }

    private long mExitTime;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出",
                        Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onResume() {
        super.onResume();
        initmapview();
    }
}
